﻿/// <reference path="primes-calculator.js" />
// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232509
(function () {
    "use strict";

    WinJS.Binding.optimizeBindingReferences = true;

    var app = WinJS.Application;
    var activation = Windows.ApplicationModel.Activation;

    app.onactivated = function (args) {
        var primesCalculator = new PrimesCalculator();

        //Calculate primes in a range
        var calculatePrimesButton = document.getElementById("calculateRange");
        var primesFirstInput = document.getElementById("rangeFrom");
        var primesLastInput = document.getElementById("rangeTo");

        calculatePrimesButton.addEventListener("click", function () {
            primesCalculator.calculatePrimesRange(primesFirstInput.value, primesLastInput.value).then(function (result) {
                var primesPlaceholder = document.getElementById("result");
                primesPlaceholder.innerText = result.join(", ");
                document.body.appendChild(primesPlaceholder);
            }, function (error) {
                var primesPlaceholder = document.getElementById("result");
                primesPlaceholder.innerText = error;
            });
        });

        //Calculate firts N prime numbers
        var calculateFirstNPrimes = document.getElementById("calculateFirstNPrimes");
        var firstNPrimesInput = document.getElementById("firstNPrimes");

        calculateFirstNPrimes.addEventListener("click", function () {
            primesCalculator.calculateFirstNPrimes(firstNPrimesInput.value).then(function (result) {
                var primesPlaceholder = document.getElementById("result");
                primesPlaceholder.innerText = result.join(", ");
                document.body.appendChild(primesPlaceholder);
            }, function (error) {
                var primesPlaceholder = document.getElementById("result");
                primesPlaceholder.innerText = error;
            });
        });

        //Calculate primes from 0 to N
        var calculatePrimesToNPrimes = document.getElementById("calculateToNPrimes");
        var toNPrimesInput = document.getElementById("toN");

        calculatePrimesToNPrimes.addEventListener("click", function () {
            primesCalculator.calculateToNPrimes(toNPrimesInput.value).then(function (result) {
                var primesPlaceholder = document.getElementById("result");
                primesPlaceholder.innerText = result.join(", ");
                document.body.appendChild(primesPlaceholder);
            }, function (error) {
                var primesPlaceholder = document.getElementById("result");
                primesPlaceholder.innerText = error;
            });
        });
    };

    app.start();
})();
